package server;

import common.*;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;

//客户端
public class ClientConnServer {
    private ObjectOutputStream oos = NowUser.oos;
    private ObjectInputStream ois = NowUser.ois;

    public void sendinfo(Message message) throws IOException{
        oos.writeObject(message);
    }

    //登录验证
    public boolean sendLoginInfoToServer(Message message){
        boolean f = false;
        try{
            System.out.println("成功发送消息到服务器端验证");
            oos.writeObject(message);
            System.out.println(message.getUser().getId());
            System.out.println(message.getType());
            Message message1 = (Message)ois.readObject();
            if(message1.getType().equals("loginac")){
                f = true;
                //建立线程加入管理
                System.out.println("登陆成功！");
            }else {
                f = false;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return f;
    }

    //注册
    public boolean sendRegisterInfoToServer(Message message){
        boolean f = false;
        try{
            oos.writeObject(message);
            Message message1 = (Message)ois.readObject();
            if(message1.getType().equals("registerac")){
                f = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return f;
    }

    //找回密码
    public boolean sendFindBackPswToServer(Message message){
        boolean f = false;
        try{
            oos.writeObject(message);
            Message message1 = (Message)ois.readObject();
            if(message1.getType().equals("checkmailac")){
                f = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(f);
        return f;
    }

    //获得当前用户信息
    public User getUser(Message message){
        Message message1 = null;
        try{
            oos.writeObject(message);
            message1 = (Message)ois.readObject();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return message1.getUser();
    }

    //获得选中的群聊信息
    public Group getGroup(Message o){
        Message msg = null;
        try {
            oos.writeObject(o);
            msg = (Message)ois.readObject();
        }catch(Exception e){
            e.printStackTrace();
        }
        System.out.println(msg.getGroup().getGroupname()+"找到啦！");
        return msg.getGroup();
    }

    //找回密码中确认
    public boolean modifypwd(Message o){
        Message msg = null;
        boolean flag = false;
        try {
            //System.out.println("成功发送消息到服务器");
            Socket s = new Socket("localhost",9999);
            oos.writeObject(o);
            msg = (Message)ois.readObject();
            if(msg.getType().equals("mpwdac")){
                flag = true;
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return flag;
    }

    //修改个人资料
    public boolean changeInformation(Message o){
        Message msg = null;
        boolean flag = false;
        try {
            System.out.println(o.getUser().getGender());
            oos.writeObject(o);
            msg = (Message)ois.readObject();
            if(msg.getType().equals("changeInformationac")){
                flag = true;
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return flag;
    }

    //修改密码的邮箱
    public boolean sendChangeEmailInfoServer(Message o) {
        boolean f=false;
        try {
            oos.writeObject(o);
            Message msg = (Message)ois.readObject();
            if(msg.getType().equals("xgcheckemailac")) {
                System.out.println("接收到修改密码");
                f = true;
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        System.out.println(f);
        return f;
    }

    //修改密码中的确认
    public boolean changepwdtrue(Message o){
        Message msg = null;
        boolean flag = false;
        try {
            oos.writeObject(o);
            msg = (Message)ois.readObject();
            if(msg.getType().equals("xgpwdac")){
                flag=true;
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        System.out.println("修改密码" + flag);
        return flag;
    }

    //添加好友
    public boolean addFriend(Message o){
        Message msg = null;
        boolean flag = false;
        try {
            //System.out.println("成功发送消息到服务器");
            //Socket s=new Socket("localhost",8888);
            oos.writeObject(o);
            msg = (Message)ois.readObject();
            if(msg.getType().equals("addfriendac")){
                flag = true;
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return flag;
    }

    //检查添加好友是否存在
    public boolean sendCheckUser(Message o) {
        boolean f=false;
        try {
            oos.writeObject(o);
            Message msg=(Message)ois.readObject();
            if(msg.getType().equals("checkuserac")) {
                System.out.println("添加好友存在");
                f=true;
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        System.out.println("w"+f);
        return f;
    }

    //是否互为好友
    public int checkrelation(Message o) {
        int f=0;
        try {
            oos.writeObject(o);
            Message msg=(Message)ois.readObject();
            if(msg.getType().equals("eachotherac")) {
                System.out.println("互为好友");
                f=1;
            }else if(msg.getType().equals("eachotherf")){
                f=2;
            }else if(msg.getType().equals("eachotherff")){
                f=3;
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        System.out.println("w"+f);
        return f;
    }

    //获取好友列表
    public ArrayList getfriends(Message o) {
        ArrayList<User> f=null;
        try {
            oos.writeObject(o);
            Message msg=(Message)ois.readObject();
            if(msg.getType().equals("getfriendsac")) {
                System.out.println("添加好友存在");
                f=msg.getFriendList();
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        //System.out.println("gg"+f);
        return f;
    }

    //获取群聊列表
    public ArrayList getgroups(Message o) {
        ArrayList<Group> f=null;
        try {
            oos.writeObject(o);
            Message msg=(Message)ois.readObject();
            if(msg.getType().equals("getgroupsac")) {
                System.out.println("我创建的群聊存在");
                f=msg.getGroupList();
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return f;
    }

    //获取我加入的群聊列表
    public ArrayList getothergroups(Message o) {
        ArrayList<Group> f=null;
        try {
            oos.writeObject(o);
            Message msg=(Message)ois.readObject();
            if(msg.getType().equals("getothergroupsac")) {
                System.out.println("我添加群聊存在");
                f=msg.getOwngroupList();
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return f;
    }

    //获取消息列表
    public ArrayList<Information> getinformation(Message o) throws IOException, ClassNotFoundException {
        ArrayList<Information> f=null;
        oos.writeObject(o);
        Message msg=(Message)ois.readObject();
        //System.out.println(msg.getType());
        if(msg.getType().equals("getinformationsac")) {
            System.out.println("添加消息存在");
            f=msg.getIlist();
            //System.out.println(f.size()+"hh");
        }
        return f;
    }

    //获取消息列表
    public ArrayList<Information> getGroupinformation(Message o) throws IOException, ClassNotFoundException {
        ArrayList<Information> f=null;
        oos.writeObject(o);
        Message msg=(Message)ois.readObject();
        //System.out.println(msg.getType());
        if(msg.getType().equals("getgroupinformationsac")) {
            System.out.println("添加群聊消息存在");
            f=msg.getQilist();
            //System.out.println(f.size()+"hh");
        }
        return f;
    }

    //删除好友
    public boolean deleteFriend(Message o){
        Message msg = null;
        boolean flag=false;
        try {
            oos.writeObject(o);
            msg=(Message)ois.readObject();
            if(msg.getType().equals("deletefriendac")){
                flag=true;
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return flag;
    }

    //同意好友请求
    public boolean acceptFriend(Message o){
        Message msg = null;
        boolean flag=false;
        try {
            oos.writeObject(o);
            msg=(Message)ois.readObject();
            if(msg.getType().equals("acceptfriendac")){
                flag=true;
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return flag;
    }

    //拒绝好友添加请求
    public boolean acceptfalseFriend(Message o){
        Message msg = null;
        boolean flag=false;
        try {
            oos.writeObject(o);
            msg=(Message)ois.readObject();
            if(msg.getType().equals("acceptfalsefriendac")){
                flag=true;
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return flag;
    }

    //同意入群请求
    public boolean acceptGroup(Message o){
        Message msg = null;
        boolean flag=false;
        try {
            oos.writeObject(o);
            msg=(Message)ois.readObject();
            if(msg.getType().equals("gapplytrueac")){
                flag=true;
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return flag;
    }

    //拒绝入群请求
    public boolean acceptfalseGroup(Message o){
        Message msg = null;
        boolean flag=false;
        try {
            oos.writeObject(o);
            msg=(Message)ois.readObject();
            if(msg.getType().equals("gapplyflaseac")){
                flag=true;
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return flag;
    }

    //判断是否在群里,是否添加过群
    public int grouprelation(Message o){
        Message msg = null;
        int flag=0;
        try {
            oos.writeObject(o);
            msg=(Message)ois.readObject();
            if(msg.getType().equals("sendedac")){
                flag=1;
            }else if(msg.getType().equals("belonggroupac")){
                flag=2;
            }else if(msg.getType().equals("grouprelationac")){
                flag=3;
            }else if(msg.getType().equals("nothisgroup")){
                flag=4;
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return flag;
    }

    //添加群聊
    public boolean addGroup(Message o){
        Message msg = null;
        boolean flag=false;
        try {
            oos.writeObject(o);
            msg=(Message)ois.readObject();
            if(msg.getType().equals("addgroupac")){
                flag=true;
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return flag;
    }

    //创建群聊
    public boolean creatgroup(Message o){
        Message msg = null;
        boolean flag=false;
        try {
            //System.out.println(o.getUser().getGroupid());
            oos.writeObject(o);
            msg=(Message)ois.readObject();
            if(msg.getType().equals("creatgroupac")){
                flag=true;
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return flag;
    }

    //退出群聊
    public boolean exitGroup(Message o){
        Message msg = null;
        boolean flag=false;
        try {
            oos.writeObject(o);
            msg=(Message)ois.readObject();
            if(msg.getType().equals("exitgroupac")){
                flag=true;
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return flag;
    }

    //解散群聊
    public boolean dissolutionGroup(Message o){
        Message msg = null;
        boolean flag=false;
        try {
            oos.writeObject(o);
            msg=(Message)ois.readObject();
            if(msg.getType().equals("dissolutiongroupac")){
                flag=true;
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        return flag;
    }
}
